package com.market.service.wx.impl;

import com.github.pagehelper.PageInfo;
import com.market.bean.MarketAddress;
import com.market.bean.MarketAddressExample;
import com.market.bean.MarketUser;
import com.market.bean.common.CommonData;
import com.market.mapper.MarketAddressMapper;
import com.market.mapper.MarketUserMapper;
import com.market.service.wx.WxAddressService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: WxAddressServiceImpl
 * @description:
 * @author: March
 * @version: 1.0
 * @datetime: 2023/1/2 19:27
 */
@Service
public class WxAddressServiceImpl implements WxAddressService {

    @Autowired
    MarketAddressMapper marketAddressMapper;
    @Autowired
    MarketUserMapper marketUserMapper;

    @Override
    public CommonData<MarketAddress> list() {

        Integer userId = null;

        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()) {
            //在已经认证成功的情况下，可以获得用户信息
            MarketUser primaryPrincipal = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
            //从primaryPrincipal中获取用户的id
            userId = primaryPrincipal.getId();
        }

        //通过id查询出对应的地址列表
        MarketAddressExample example = new MarketAddressExample();
        example.createCriteria().andIdEqualTo(userId).andDeletedEqualTo(false);

        List<MarketAddress> list = marketAddressMapper.selectByExample(example);

        PageInfo<MarketAddress> pageInfo = new PageInfo<>(list);

        return CommonData.data(pageInfo);
    }


    @Transactional
    @Override
    public int save(Map map) {
        //获取用户id
        Integer userId = null;
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()) {
            PrincipalCollection principals = subject.getPrincipals();
            MarketUser marketUser = (MarketUser) principals.getPrimaryPrincipal();
            userId = marketUser.getId();
        }

        //封装请求参数
        /*addressDetail: "666"
            areaCode: "120103"
            city: "市辖区"
            county: "河西区"
            id: 0  //新增时默认为0,修改时为数据库地址表中的Id
            isDefault: 0
            name: "六六六"
            province: "天津市"
            tel: "13200000000"*/
        MarketAddress marketAddress = new MarketAddress();

        marketAddress.setAddressDetail((String) map.get("addressDetail"));
        marketAddress.setAreaCode((String) map.get("areaCode"));
        marketAddress.setCity((String) map.get("city"));
        marketAddress.setCounty((String) map.get("county"));

        // 当请求参数中的isDefault的值为false时,进行正常的添加与修改
        // 若值为true,则将所有地址的idDefault的值改为false,然后再将本次增加修改的地址的idDefault的值改为true
        if ((boolean)map.get("isDefault")){
            int code = marketAddressMapper.updateIsDefault(userId);
            marketAddress.setIsDefault(true);
        }else {
            marketAddress.setIsDefault(false);
        }

        marketAddress.setName((String) map.get("name"));
        marketAddress.setProvince((String) map.get("province"));
        marketAddress.setTel((String) map.get("tel"));
        // TODO: 2023/1/2 此处需要根据地址获得邮编,并将邮编写入数据库
        // marketAddress.setPostalCode("邮政编码");

        Date date = new Date();
        marketAddress.setUpdateTime(date);
        marketAddress.setUserId(userId);

        Integer addressId = (Integer) map.get("id");
        int code;
        //当addressId为0时,表示新增地址信息,不为0则表示修改地址信息
        if("0".equals(addressId)){
            //新增地址
            marketAddress.setAddTime(date);
            code = marketAddressMapper.insertSelective(marketAddress);
        }else{
            marketAddress.setId(addressId);
            code = marketAddressMapper.updateByPrimaryKeySelective(marketAddress);
        }
        return code;
    }

    @Override
    public MarketAddress detail(Integer id) {
        MarketAddress marketAddress = marketAddressMapper.selectByPrimaryKey(id);
        return marketAddress;
    }

    @Override
    public int delete(Map map) {
        Integer addressId = (Integer) map.get("id");
        MarketAddress marketAddress = new MarketAddress();
        marketAddress.setId(addressId);
        marketAddress.setDeleted(false);
        int code = marketAddressMapper.updateByPrimaryKeySelective(marketAddress);
        return code;
    }
}
